package com.xbdun.comweb.bmp.redis;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.xbdun.comweb.bmp.util.UserInfoUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class XbdunRedisTemplate {
	private static final String KEY_SPLIT = ":"; //用于隔开缓存前缀与缓存键值
	@Autowired
    private StringRedisTemplate stringRedisTemplate;

	/**
     * 设置缓存 
     * @param prefix 缓存前缀（用于区分缓存，防止缓存键值重复）
     * @param key    缓存key
     * @param value  缓存value
     */
    public void set(String prefix, String key, String value) {
    	if(stringRedisTemplate.isExposeConnection()){
    		stringRedisTemplate.opsForValue().set(prefix + KEY_SPLIT + key, value);
    	}else{
    		log.info("采用的WebSession缓存:"+(prefix + KEY_SPLIT + key));
    		UserInfoUtil.getCurrentRequest().getSession().setAttribute(prefix + KEY_SPLIT + key, value);
    	}
    }

    /**设置过期时间*/
    public void expire(String prefix, String key,int time){
    	if(stringRedisTemplate.isExposeConnection())
    	stringRedisTemplate.expire(prefix + KEY_SPLIT + key, time,TimeUnit.SECONDS);
    }
    
    /**
     * 设置缓存，并且自己指定过期时间
     * @param prefix
     * @param key
     * @param value
     * @param expireTime 过期时间
     */
    public void setWithExpireTime(String prefix, String key, String value, int expireTime) {
    	set(prefix, key, value);
    	expire(prefix, key,expireTime);
        log.debug("RedisUtil:setWithExpireTime cache key={},value={},expireTime={}", prefix + KEY_SPLIT + key, value,expireTime);
    }
    /**
     * 获取指定key的缓存
     * @param prefix
     * @param key
     */
    public String get(String prefix, String key) {
    	String value = null;
    	if(stringRedisTemplate.isExposeConnection()){
    		value = stringRedisTemplate.opsForValue().get(prefix + KEY_SPLIT + key);
    	}else{
    		value = (String) UserInfoUtil.getCurrentRequest().getSession().getAttribute(prefix + KEY_SPLIT + key);
    	}
        log.debug("RedisUtil:get cache key={},value={}", prefix + KEY_SPLIT + key, value);
        return value;
    }
    
    /**
     * 删除指定key的缓存
     * @param prefix
     * @param key
     */
    public void deleteWithPrefix(String prefix, String key) {
    	if(stringRedisTemplate.isExposeConnection()){
    		stringRedisTemplate.delete(prefix + KEY_SPLIT + key);
    	}else{
    		UserInfoUtil.getCurrentRequest().getSession().removeAttribute(prefix + KEY_SPLIT + key);
    	}
        log.debug("RedisUtil:delete cache key={}", prefix + KEY_SPLIT + key);
    }
    
    public void delete(String key) {
    	if(stringRedisTemplate.isExposeConnection()){
    		stringRedisTemplate.delete(key);
    	}else{
    		UserInfoUtil.getCurrentRequest().getSession().removeAttribute(key);
    	}
        log.debug("RedisUtil:delete cache key={}", key);
    }
    
    /**是否存在某KEY**/
    public boolean existeKey(String prefix, String key){
    	if(stringRedisTemplate.isExposeConnection()){
    		return stringRedisTemplate.hasKey(prefix + KEY_SPLIT + key);
    	}else{
    		return (UserInfoUtil.getCurrentRequest().getSession().getAttribute(prefix + KEY_SPLIT + key)!=null);
    	}
    }
}
